package state.person;

import java.io.Serializable;

import state.group.HallCall;

public class Person implements Serializable {
    
    private static final long serialVersionUID = -1082525875606839910L;
    
    private String name;
    private PersonType type;
    private int weight;
    private HallCall call;
    private byte destFloor;
    
    private PersonState state;
    private byte curElevatorId;
    
    private transient boolean isBusy;
    
    public Person(String name, PersonType type, int weight, HallCall call, byte destFloor)
    {
        this.name = name;
        this.type = type;
        this.weight = weight;
        this.call = call;
        this.destFloor = destFloor;
        
        this.state = PersonState.waiting;
        this.curElevatorId = 0;
    }
    
    public String getName()
    {
        return name;
    }
    
    public PersonType getType()
    {
        return type;
    }
    
    public int getWeight()
    {
        return weight;
    }
    
    public HallCall getHallCall()
    {
        return call;
    }
    
    public byte getDestination()
    {
        return destFloor;
    }
    
    public PersonState getState()
    {
        return state;
    }
    
    public void setState(PersonState state)
    {
        this.state = state;
    }
    
    public byte getCurrentElevatorId()
    {
        return this.curElevatorId;
    }
    
    public void setCurrentElevatorId(byte id)
    {
        this.curElevatorId = id;
    }
    
    public boolean isFree()
    {
        return !isBusy;
    }
    
    public void setBusy()
    {
        isBusy = true;
    }
    
    public void setFree()
    {
        isBusy = false;
    }
    
    @Override
    public String toString()
    {
        return "<Person name=\"" + name + "\" type=" + type + " weight=" + weight + 
                " call=" + call + " destFloor=" + destFloor + " state=" + state + 
                " curElevatorId=" + curElevatorId + "/>";
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        
        if (o instanceof Person)
        {
            Person other = (Person) o;
            
            if (! this.name.equals( other.name ))
            {
                return false;
            }
            
            if (this.type != other.type)
            {
                return false;
            }
            
            if (this.weight != other.weight)
            {
                return false;
            }
            
            if (! this.call.equals(other.call))
            {
                return false;
            }
            
            if (this.destFloor != other.destFloor)
            {
                return false;
            }
            
            if (this.state != other.state)
            {
                return false;
            }
            
            if (this.curElevatorId != other.curElevatorId)
            {
                return false;
            }
            
            return true;
        }
        
        return false;
    }
    
    @Override
    public int hashCode()
    {
        int retVal = 0;
        
        retVal += name.hashCode();
        retVal += type.hashCode();
        retVal += state.hashCode();
        retVal += call.hashCode() * 32;
        retVal += this.curElevatorId * 96;
        retVal += this.destFloor * 512;
        retVal += this.weight * 2048;
                
        return retVal;
    }
    
}
